package com.iretailer.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.base.BaseController;
import com.iretailer.model.Device;
import com.iretailer.model.Item;
import com.iretailer.model.Note;
import com.iretailer.model.SiteDevice;
import com.iretailer.util.ResponeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.*;

@RestController
public class ResolveController extends BaseController {
    private Logger log = LoggerFactory.getLogger(ResolveController.class);

    @PostMapping(value = "/resolve")
    public void resolve(HttpServletResponse response, HttpServletRequest request, @RequestBody Map<String, Object> map) {
        String post = JSON.toJSONString(map);
        JSONObject object = JSON.parseObject(post);
        String deviceName = object.getString("deviceName");
        String siteName = object.getString("siteName");
        log.info("resolve post : " + post);
        SiteDevice device = deviceService.selectSiteDeviceBySiteName_DeviceName(siteName, deviceName);
        if (device != null) {
            String dateTime = object.getString("dateTime");
            LocalDateTime date = LocalDateTime.ofInstant(ZonedDateTime.parse(dateTime).toInstant(), ZoneOffset.systemDefault());

            JSONObject datas = object.getJSONObject("data");
            if (datas != null) {
                List<Item> updateItem = new ArrayList<>();
                List<Note> notes = new ArrayList<>();
                List<String> epcs = new ArrayList<>(datas.keySet());
                for (int i = 0; i < epcs.size(); i++) {
                    Integer times = datas.getInteger(epcs.get(i));
                    Item item = itemService.getOne(new QueryWrapper<Item>().lambda().eq(Item::getEpc, epcs.get(i)));
//                    Note note = new Note(device.getSiteId(), device.getDeviceId(),date, times, epcs.get(i));
                    if (item != null) {
                        Note note = new Note(item.getId(), date, times);
                        notes.add(note);
                        item.setUpdateTime(date);
                        updateItem.add(item);
                    } else {
                        log.error("epcs not find " + epcs.get(i));
                    }
                }
                if (notes.size() > 0) {
                    notesService.saveBatch(notes);
                }
                if(updateItem.size()>0){
                    itemService.updateBatchById(updateItem);
                }
                device.setUpdateTime(LocalDateTime.now());
                Device updateTime = new Device();
                updateTime.setId(device.getDeviceId());
                updateTime.setUpdateTime(LocalDateTime.now());
                deviceService.updateById(updateTime);
            }
            log.info("resolve respone 200");
            ResponeUtil.sendSuccess(response, "ok");
        } else {
            log.info("resolve respone 404");
            response.setStatus(404);
            ResponeUtil.sendFail(response, "not find");
        }

    }

}
